package websocket;

import cn.hutool.cron.CronUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import websocket.netty.NettyServer;
import websocket.netty.SocketProperties;

import javax.annotation.Resource;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 功能描述: 任务队列
 *  8090 基站
 *  8091 地面站
 *  8093 无人机站
 */
@Component
@Slf4j
public class LaunchRunner implements CommandLineRunner {



    @Resource
    private NettyServer nettyServer;
    @Resource
    private SocketProperties socketProperties;


    @Override
    public void run(String... args) throws Exception {
        TaskRunner();
        InetSocketAddress addressJz = new InetSocketAddress(socketProperties.getHost(),socketProperties.getPortJz());
        InetSocketAddress addressDmz = new InetSocketAddress(socketProperties.getHost(),socketProperties.getPortDMZ());
        InetSocketAddress addressWrjz = new InetSocketAddress(socketProperties.getHost(),socketProperties.getPortWrjz());
        ExecutorService pool = Executors.newFixedThreadPool(4);
        log.info("netty服务器启动地址:"+socketProperties.getHost());

        //服务端启动
        pool.execute(new Runnable() {
            @Override
            public void run() {
                nettyServer.startJz(addressJz);
            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                nettyServer.startDmz(addressDmz);

            }
        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                nettyServer.startWrjz(addressWrjz);

            }
        });



    }
    /**
     * 执行正在运行的任务
     */
    private  void TaskRunner() {
        /**
         * 任务队列启动
         */
        CronUtil.setMatchSecond(true);
        CronUtil.start();
        log.info("\n-----------------------任务服务启动------------------------\n\t" +
                        "当前正在启动的{}个任务"+
                        "\n-----------------------------------------------------------\n\t"
                , CronUtil.getScheduler().size()

        );
    }


}
